package com.haixiaoke.saas.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.system.domain.SysArea;
import com.haixiaoke.saas.system.mapper.SysAreaMapper;
import com.haixiaoke.saas.system.service.ISysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 区域服务层实现
 *
 * @author qixi
 * @date 2023/10/23
 */
@Service
public class SysAreaServiceImpl implements ISysAreaService {

    @Autowired
    private SysAreaMapper sysAreaMapper;


    @Override
    public List<SysArea> list(SysArea sysArea) {
        List<SysArea> list = new ArrayList<>();
        Integer level = sysArea.getLevel();


        if (level == null || level > 5 || level < 1) {
            throw new ServiceException("level参数错误");
        }

        if (level != 1 && StrUtil.isBlank(sysArea.getParentId())) {
            throw new ServiceException("parentId参数不能为空");
        }

        switch (level) {
            case 1:
                list = sysAreaMapper.selectProvinceList(sysArea);
                break;
            case 2:
                list = sysAreaMapper.selectCityList(sysArea);
                break;
            case 3:
                list = sysAreaMapper.selectAreaList(sysArea);
                break;
            case 4:
                list = sysAreaMapper.selectStreetList(sysArea);
                break;
            case 5:
                list = sysAreaMapper.selectVillageList(sysArea);
                break;
            default:
                break;
        }

        return list;
    }
}
